import { defineStore } from 'pinia';
import { components } from '@/types/api';
import { ref, type Ref } from 'vue';

// 定义类型
type UserType = components['schemas']['SchemaUserOpen'];
type AuthType = components['schemas']['SchemaUserTokens'];

// 定义状态接口
interface ServerState {
    user: Ref<UserType | undefined>;
    auth: Ref<AuthType | undefined>;
    device: Ref<string>;
    theme: Ref<string>;
    navbar: Ref<boolean>;
}

// 定义 store
export const useStore = defineStore(
    'store',
    (): ServerState => {
        // 初始化语言设置
        if (!localStorage.getItem('lang')) {
            localStorage.setItem('lang', 'zh-CN');
        }

        // 定义响应式状态
        const user = ref<UserType>();
        const auth = ref<AuthType>();
        const device = ref<string>('desktop');
        const theme = ref<string>('dark');
        const navbar = ref<boolean>(true);
        
        return {
            user,
            auth,
            device,
            theme,
            navbar
        };
    },
    {
        persist: {
            storage: localStorage,
            paths: ['user', 'auth']
        },
        
    }
);